package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.api.entity.BasicPersonalSettingsEntity;
import com.iwomy.secureplat.platform.service.BasicPersonalSettingsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 个人设置
 *
 * @author pig
 * @date 2023-11-08 10:41:35
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicPersonalSettings")
@Tag(description = "basicPersonalSettings", name = "个人设置管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicPersonalSettingsController {

	private final BasicPersonalSettingsService basicPersonalSettingsService;

	/**
	 * 分页查询
	 *
	 * @param page                  分页对象
	 * @param basicPersonalSettings 个人设置
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_basicPersonalSettings_view')")
	public R getBasicPersonalSettingsPage(@ParameterObject Page page, @ParameterObject BasicPersonalSettingsEntity basicPersonalSettings) {
		LambdaQueryWrapper<BasicPersonalSettingsEntity> wrapper = Wrappers.lambdaQuery();
		return R.ok(basicPersonalSettingsService.page(page, wrapper));
	}


	/**
	 * 通过id查询个人设置
	 *
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping()
	public R getCurrent() {
		BasicPersonalSettingsEntity basicPersonalSettingsEntity = new BasicPersonalSettingsEntity();
		basicPersonalSettingsEntity.setUserId(SecurityUtils.getUser().getId());
		LambdaQueryWrapper<BasicPersonalSettingsEntity> wrapper = Wrappers.lambdaQuery(basicPersonalSettingsEntity);
		BasicPersonalSettingsEntity one = basicPersonalSettingsService.getOne(wrapper);
		return R.ok(ObjectUtil.isNotNull(one) ? one : new BasicPersonalSettingsEntity());
	}

	@GetMapping("/getById/{userId}")
	public R getById(@PathVariable("userId") Long userId) {
		BasicPersonalSettingsEntity basicPersonalSettingsEntity = new BasicPersonalSettingsEntity();
		basicPersonalSettingsEntity.setUserId(userId);
		LambdaQueryWrapper<BasicPersonalSettingsEntity> wrapper = Wrappers.lambdaQuery(basicPersonalSettingsEntity);
		BasicPersonalSettingsEntity one = basicPersonalSettingsService.getOne(wrapper);
		return R.ok(ObjectUtil.isNotNull(one) ? one : new BasicPersonalSettingsEntity());
	}

	/**
	 * 新增个人设置
	 *
	 * @param basicPersonalSettings 个人设置
	 * @return R
	 */
	@Operation(summary = "新增个人设置", description = "新增个人设置")
	@SysLog("新增个人设置")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_basicPersonalSettings_add')")
	public R save(@RequestBody BasicPersonalSettingsEntity basicPersonalSettings) {
		return R.ok(basicPersonalSettingsService.save(basicPersonalSettings));
	}

	/**
	 * 修改个人设置
	 *
	 * @param basicPersonalSettings 个人设置
	 * @return R
	 */
	@Operation(summary = "修改个人设置", description = "修改个人设置")
	@SysLog("修改个人设置")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicPersonalSettings_edit')")
	public R updateById(@RequestBody BasicPersonalSettingsEntity basicPersonalSettings) {
		return R.ok(basicPersonalSettingsService.updateById(basicPersonalSettings));
	}

	/**
	 * 新增或修改
	 *
	 * @param basicPersonalSettings 个人设置
	 * @return R
	 */
	@Operation(summary = "新增或修改", description = "新增或修改")
	@SysLog("新增或修改")
	@PostMapping("/addOrUpdate")
//	@PreAuthorize("@pms.hasPermission('platform_basicPersonalSettings_addOrUpdate')" )
	public R addOrUpdate(@RequestBody BasicPersonalSettingsEntity basicPersonalSettings) {
		return R.ok(basicPersonalSettingsService.addOrUpdate(basicPersonalSettings));
	}


	/**
	 * 通过id删除个人设置
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除个人设置", description = "通过id删除个人设置")
	@SysLog("通过id删除个人设置")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicPersonalSettings_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(basicPersonalSettingsService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicPersonalSettings 查询条件
	 * @param ids                   导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicPersonalSettings_export')")
	public List<BasicPersonalSettingsEntity> export(BasicPersonalSettingsEntity basicPersonalSettings, Long[] ids) {
		return basicPersonalSettingsService.list(Wrappers.lambdaQuery(basicPersonalSettings).in(ArrayUtil.isNotEmpty(ids), BasicPersonalSettingsEntity::getId, ids));
	}
}